<template>
  <div>
    <div>
      <!--    现在没法返回userRole好像-->
      <h2 style="text-align: center; ">个人信息</h2>
    </div>
    <!--个人信息-->
    <div>
      <el-form :model="userForm" status-icon
               :rules="userFormRules"
               ref="userForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="姓名:" prop="name">
          {{userInfo.name}}
        </el-form-item>
        <el-form-item label="身份:" prop="userRole">
          <el-tag type="info">{{userInfo.userRole}}</el-tag>
        </el-form-item>
        <!--          状态还没写好-->
        <el-form-item label="状态:" prop="status">
          <el-tag type="success">{{userInfo.statu}}</el-tag>
        </el-form-item>
        <el-form-item label="学工号:" prop="username">
          {{userInfo.username}}
        </el-form-item>
        <el-form-item label="身份证号:" prop="identificationNumber">
          {{ userInfo.identificationNumber}}
        </el-form-item>
        <el-form-item label="院系:" prop="collegeName">
          {{userInfo.collegeName}}
        </el-form-item>
        <el-form-item label="专业:" prop="majorName">
          {{userInfo.majorName}}
        </el-form-item>
        <el-form-item label="手机号:" prop="phoneNumber">
          <el-input v-model="userForm.phoneNumber" autocomplete="off" ></el-input>
        </el-form-item>
        <el-form-item label="邮箱:" prop="email">
          <el-input v-model="userForm.email" autocomplete="off" ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <!--按钮-->
    <div slot="footer" class="buttons">
      <el-button @click="resetForm('userForm')">取 消</el-button>
      <el-button type="primary" @click="submitUserForm('userForm')">保存</el-button>
      <el-button type="text" @click="dialogFormVisible= true" style="margin-top: 10px" >修改密码</el-button>
    </div>
    <!--  修改密码对话框-->
    <div>
      <el-dialog
          title="修改密码"
          :visible.sync="dialogFormVisible"
          width="600px"
          @closed="resetForm('passForm')">

        <el-form :model="passForm" status-icon :rules="passFormRules" ref="passForm" label-width="100px">
          <el-form-item label="旧密码" prop="currentPass">
            <el-input type="password" v-model="passForm.currentPass" autocomplete="off"></el-input>
          </el-form-item>
          <br>
          <el-form-item label="新密码" prop="password">
            <el-input type="password" v-model="passForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <br>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="passForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <!--按钮-->
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitPassForm('passForm')">提交</el-button>
          <el-button @click="resetForm('passForm')">重置</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "UserCenter",data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.passForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      userInfo:{},
      userForm:{},
      //表单规则
      userFormRules: {
        phoneNumber: [
          {required: false, message: '请输入手机号', trigger: 'blur'},
          { pattern: /^1[3456789]\d{9}$/, message: "手机号码格式不正确", trigger: "blur" }
        ],
        email: [
          {required: false,message: '请输入邮箱', trigger: 'blur'},
          { pattern: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,message:"邮箱格式不正确",trigger:"blur"}
        ],
      },
      //修改密码可视窗
      dialogFormVisible: false,
      //修改密码表单
      passForm: {
        password: '',
        checkPass: '',
        currentPass: ''
      },
      //修改密码表单规则
      passFormRules: {
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern:/(?!.*\s)(?!^[\u4e00-\u9fa5]+$)(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{6,32}$/,
            message: "长度为6-32个字符字母，数字或者特殊字符（-_）至少包含两种",trigger:"blur"}
        ],
        checkPass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        currentPass: [
          { required: true, message: '请输入当前密码', trigger: 'blur' },
        ]
      }

    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    // 获取用户信息
    getUserInfo() {
      this.$axios.get("/sys/user/currentInfo",).then(res => {
        this.userInfo = res.data.data;
        this.userForm = res.data.data;
        console.log(res.data.data);
      })
    },
    //提交个人信息表单
    submitUserForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/sys/user/update/admin', this.userForm).then(res => {
            console.log(res.data)
            // this.resetForm(formName)
            this.$message({
              showClose: true,
              message: '恭喜你，操作成功',
              type: 'success',
              onClose: () => {
                this.getUserInfo()
              }
            });
          })
        } else {
          console.log('error submit!');
          return false;
        }
      });
    },
    //提交密码表单
    submitPassForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          this.$axios.post('/sys/user/updatePass', this.passForm).then(res => {
            _this.$alert(res.data.msg, '提示', {
              confirmButtonText: '确定',
              callback: action => {
                this.$refs[formName].resetFields();
                this.$axios.post("/logout").then(res => {
                  console.log(res.data.data)
                  //退出要删除数据
                  localStorage.clear()
                  sessionStorage.clear()
                  this.$store.commit("resetState")
                  this.$router.push("/login")
                })
              }
            });
          })
          this.dialogFormVisible = false;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.el-form {
  width: 400px;
  margin: 0 auto;
}
.buttons{
  text-align: center;
}

</style>